package com.hanxing.kxcharge.module.operations.controller.admin.equipmentconnector;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.CommonResult;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;
import static com.hanxing.kxcharge.framework.common.pojo.CommonResult.success;

import com.hanxing.kxcharge.framework.excel.core.util.ExcelUtils;

import com.hanxing.kxcharge.framework.operatelog.core.annotations.OperateLog;
import static com.hanxing.kxcharge.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hanxing.kxcharge.module.operations.controller.admin.equipmentconnector.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.equipmentconnector.EquipmentConnectorDO;
import com.hanxing.kxcharge.module.operations.service.equipmentconnector.EquipmentConnectorService;

@Tag(name = "管理后台 - 电枪管理")
@RestController
@RequestMapping("/operations/equipment-connector")
@Validated
public class EquipmentConnectorController {

    @Resource
    private EquipmentConnectorService equipmentConnectorService;

    @PostMapping("/create")
    @Operation(summary = "创建电枪管理")
    @PreAuthorize("@ss.hasPermission('operations:equipment-connector:create')")
    public CommonResult<Long> createEquipmentConnector(@Valid @RequestBody EquipmentConnectorSaveReqVO createReqVO) {
        return success(equipmentConnectorService.createEquipmentConnector(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电枪管理")
    @PreAuthorize("@ss.hasPermission('operations:equipment-connector:update')")
    public CommonResult<Boolean> updateEquipmentConnector(@Valid @RequestBody EquipmentConnectorSaveReqVO updateReqVO) {
        equipmentConnectorService.updateEquipmentConnector(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电枪管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('operations:equipment-connector:delete')")
    public CommonResult<Boolean> deleteEquipmentConnector(@RequestParam("id") Long id) {
        equipmentConnectorService.deleteEquipmentConnector(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电枪管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('operations:equipment-connector:query')")
    public CommonResult<EquipmentConnectorRespVO> getEquipmentConnector(@RequestParam("id") Long id) {
        EquipmentConnectorDO equipmentConnector = equipmentConnectorService.getEquipmentConnector(id);
        return success(BeanUtils.toBean(equipmentConnector, EquipmentConnectorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电枪管理分页")
    @PreAuthorize("@ss.hasPermission('operations:equipment-connector:query')")
    public CommonResult<PageResult<EquipmentConnectorRespVO>> getEquipmentConnectorPage(@Valid EquipmentConnectorPageReqVO pageReqVO) {
        PageResult<EquipmentConnectorDO> pageResult = equipmentConnectorService.getEquipmentConnectorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentConnectorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电枪管理 Excel")
    @PreAuthorize("@ss.hasPermission('operations:equipment-connector:export')")
    @OperateLog(type = EXPORT)
    public void exportEquipmentConnectorExcel(@Valid EquipmentConnectorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentConnectorDO> list = equipmentConnectorService.getEquipmentConnectorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电枪管理.xls", "数据", EquipmentConnectorRespVO.class,
                        BeanUtils.toBean(list, EquipmentConnectorRespVO.class));
    }

}